/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.OperationSubjectDetail;
import com.kaaction.modules.mer.service.OperationSubjectDetailService;

/**
 * 运营专题明细Controller
 * @author huanghaiming
 * @version 2017-07-04
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/operationSubjectDetail")
public class OperationSubjectDetailController extends BaseController {

	@Autowired
	private OperationSubjectDetailService operationSubjectDetailService;
	
	@ModelAttribute
	public OperationSubjectDetail get(@RequestParam(required=false) String id) {
		OperationSubjectDetail entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = operationSubjectDetailService.get(id);
		}
		if (entity == null){
			entity = new OperationSubjectDetail();
		}
		return entity;
	}
	
	@RequiresPermissions("mer:operationSubjectDetail:view")
	@RequestMapping(value = {"list", ""})
	public String list(OperationSubjectDetail operationSubjectDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		operationSubjectDetailService.updateOpeSubtitle(operationSubjectDetail);
		Page<OperationSubjectDetail> page = operationSubjectDetailService.findPage(new Page<OperationSubjectDetail>(request, response), operationSubjectDetail); 
		model.addAttribute("page", page);
		return "modules/mer/operationSubjectDetailList";
	}

	@RequiresPermissions("mer:operationSubjectDetail:view")
	@RequestMapping(value = "form")
	public String form(OperationSubjectDetail operationSubjectDetail, Model model) {
		model.addAttribute("operationSubjectDetail", operationSubjectDetail);
		return "modules/mer/operationSubjectDetailForm";
	}

	@RequiresPermissions("mer:operationSubjectDetail:edit")
	@RequestMapping(value = "save")
	public String save(OperationSubjectDetail operationSubjectDetail, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, operationSubjectDetail)){
			return form(operationSubjectDetail, model);
		}
		operationSubjectDetailService.save(operationSubjectDetail);
		addMessage(redirectAttributes, "保存运营专题明细成功");
		return "redirect:"+Global.getAdminPath()+"/mer/operationSubjectDetail/?repage";
	}
	
	@RequiresPermissions("mer:operationSubjectDetail:edit")
	@RequestMapping(value = "delete")
	public String delete(OperationSubjectDetail operationSubjectDetail, RedirectAttributes redirectAttributes) {
		operationSubjectDetailService.delete(operationSubjectDetail);
		addMessage(redirectAttributes, "删除运营专题明细成功");
		return "redirect:"+Global.getAdminPath()+"/mer/operationSubjectDetail/?repage&opesubId="+operationSubjectDetail.getOpesubId()+"&opeSubtitle="+operationSubjectDetail.getOpeSubtitle();
	}
	
	@RequiresPermissions("mer:operationSubjectDetail:edit")
	@RequestMapping(value = "insertOperaSubDtl")
	@ResponseBody
	public Map<String,Object> insertOperaSubDtl(String id, String opesubId) {
		Map<String,Object> map = new HashMap<String,Object>();
		operationSubjectDetailService.insertOperaSubDtl(id,opesubId);
		map.put("msg", "添加成功");
		return map;
	}
	
}